from datetime import datetime, timedelta
import uvicorn
from fastapi import FastAPI, Body, Cookie
from fastapi.responses import JSONResponse
from app import app

@app.post("/set_cookie")
def set_cookie_demo(user_id: int = Body(description="用户ID")):
    resp = JSONResponse(content={"user_id": user_id, "demo": "set_cookie"})
    max_age = int(timedelta(hours=6).total_seconds())  # cookie 有效期
    resp.set_cookie(key="user_id", value=str(user_id), max_age=max_age)
    return resp

@app.get("/get_cookie")
def get_cookie_demo(user_id: int = Cookie(default=0)):
    print("user_id", user_id)
    resp = JSONResponse(content={"user_id": user_id, "demo": "get_cookie"})
    return resp
